﻿using System;

using ApplicationLibrary;
using ApplicationLibrary.SqlConnector;

namespace Clothes.Sales.ModuleData
{
    internal class RegisterModule : ApplicationLibrary.SqlConnector.RegisterModule
    {
        public override string Application()
        {
            return "CLOT";
        }

        public override string Module()
        {
            return "SALE";
        }

        public override int CurrentVersion()
        {
            return 1;
        }

        public override Version DllVersion
        {
            get
            {
                return System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
            }
        }

        public override void RegisterCountersAndCodes()
        {
            GlobalInfo.AddCounter(Counters.Invoice.Int(), Properties.Resources.CN_Invoice);
            GlobalInfo.AddCounter(Counters.ReturnToCustomer.Int(), Properties.Resources.CN_ReturnToCustomer);
            GlobalInfo.AddCounter(Counters.BillOfLading.Int(), Properties.Resources.CN_BillOfLading);
        }

        public override void AddScrounger(ApplicationLibrary.DBManager.iScrounger frm, NameSpace nameSpace)
        {
            if (nameSpace.ApplicationPath == "Clothes.Masters.Forms.Masters")
                frm.ScroungerList.Add(new Clothes.Sales.Scrounger.MastersScrounger(frm));
        }

        public override ApplicationLibrary.Preferences.PreferencePanel[] RegisterPreferences()
        {
            //return new ApplicationLibrary.Preferences.PreferencePanel[]
            //{
            //    new AccountingPreferencePanel()
            //};
            return null;
        }

        protected override bool CreateDBTables()
        {
            SqlCreateTable newTable = new SqlCreateTable(SqlABConnection);
            #region CL_Sales
            if (SearchTable(CL_Sales.Name))
            {
                newTable.NewTable(CL_Sales.Name);

                newTable.AddColumn(CL_Sales.SaledocType);
                newTable.AddColumn(CL_Sales.DocumentNo);
                newTable.AddColumn(CL_Sales.ReleaseDate);
                newTable.AddColumn(CL_Sales.CustID);
                newTable.AddColumn(CL_Sales.Description);
                newTable.AddColumn(CL_Sales.Address);
                newTable.AddColumn(CL_Sales.ZipCode);
                newTable.AddColumn(CL_Sales.City);
                newTable.AddColumn(CL_Sales.County);
                newTable.AddColumn(CL_Sales.FiscalCode);
                newTable.AddColumn(CL_Sales.Printed);

                newTable.AddPrimaryKey(CL_Sales.SaledocType, CL_Sales.DocumentNo);
                //newTable.AddSecondaryKey("Father", CL_Categories.CodeFather, CL_Categories.Code);

                newTable.Create();
            }
            #endregion

            #region CL_SalesDetail
            if (SearchTable(CL_SalesDetail.Name))
            {
                newTable.NewTable(CL_SalesDetail.Name);

                newTable.AddColumn(CL_SalesDetail.SaledocType);
                newTable.AddColumn(CL_SalesDetail.DocumentNo);
                newTable.AddColumn(CL_SalesDetail.Line);
                newTable.AddColumn(CL_SalesDetail.ItemID);
                newTable.AddColumn(CL_SalesDetail.Price);

                newTable.AddPrimaryKey(CL_SalesDetail.SaledocType, CL_SalesDetail.DocumentNo, CL_SalesDetail.Line);
                //newTable.AddSecondaryKey("Father", CL_Categories.CodeFather, CL_Categories.Code);

                newTable.Create();
            }
            #endregion

            #region CL_CounterSales
            if (SearchTable(CL_CounterSales.Name))
            {
                newTable.NewTable(CL_CounterSales.Name);

                newTable.AddColumn(CL_CounterSales.DocumentNo);
                newTable.AddColumn(CL_CounterSales.ReleaseDate);
                newTable.AddColumn(CL_CounterSales.CustID);

                newTable.AddPrimaryKey(CL_CounterSales.DocumentNo);

                newTable.Create();
            }
            #endregion

            #region CL_CounterSalesDetail
            if (SearchTable(CL_CounterSalesDetail.Name))
            {
                newTable.NewTable(CL_CounterSalesDetail.Name);

                newTable.AddColumn(CL_CounterSalesDetail.DocumentNo);
                newTable.AddColumn(CL_CounterSalesDetail.Line);
                newTable.AddColumn(CL_CounterSalesDetail.ItemID);
                newTable.AddColumn(CL_CounterSalesDetail.Price);

                newTable.AddPrimaryKey(CL_CounterSalesDetail.DocumentNo, CL_CounterSalesDetail.Line);

                newTable.Create();
            }
            #endregion
            return true;
        }

        protected override bool UpdateDBTables()
        {
            //if (dbVersion < 2)
            //{
            //    SqlCreateTable alterMaster = new SqlCreateTable(SqlABConnection);

            //    if (!SearchColumn(FG_Masters.Email))
            //    {
            //        alterMaster.AlterTable(FG_Masters.Name);
            //        alterMaster.AddColumn(FG_Masters.Email);
            //        alterMaster.Alter();
            //    }
            //}

            return true;
        }
    }
}